package net.chanibal.hala.client;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.SerializableException;


public class RegisterForm extends AbstractForm implements AsyncCallback
{
	TextField login;
	PasswordField pass;
	TextField age;
	TextField sex;
	TextField address;
	TextField name;
	TextField familyName;
	
	RegisterForm()
	{
		super("Zarejstruj");
		login=new TextField("Login","[A-Za-z0-9]+");
		addField(login);
		pass = new PasswordField("Hasło",6,32);
		addField(pass);
		age =new TextField("Rok Urodzenia","[12][0-9]{3}");
		addField(age);
		sex =new TextField("Płeć(K lub M)","[KM]");
		addField(sex);
		address =new TextField("Adres zamieszkania",".*");
		addField(address);
		name =new TextField("Imię","(([A-ZŚŻĆŁ][a-zęóąśłżć]+[ -]*)+)+");
		addField(name);
		familyName =new TextField("Nazwisko","([A-ZŚŻĆŁ][a-zęóąśłżć]+[ -]*)+"); 
		addField(familyName);
	}

	public void doRequest()
	{
		Server.Util.getInstance().doRegister(
				login.getText(), 
				pass.getText(), 
				Integer.valueOf(age.getText()),
				sex.getText().charAt(0), 
				address.getText(), 
				name.getText(), 
				familyName.getText(), 
				this
				);
	}

	public void onFailure(Throwable caught) {
	     try {
		       throw caught;
	     } catch (IncompatibleRemoteServiceException e) {
	       // this client is not compatible with the server; cleanup and refresh the 
	       // browser
	    	 setError(e.toString());
	     } catch (InvocationException e) {
	       // the call didn't complete cleanly
	    	 setError("Błąd połączenia:"+e.toString());
	     } catch (SerializableException e) {
	       // one of the 'throws' from the original method
	    	 setError(e.getMessage());
	     } catch (Throwable e) {
	       // last resort -- a very unexpected exception
	    	 setError(e.getLocalizedMessage());
	     }
	}

	public void onSuccess(Object result) {
		History.onHistoryChanged("Login");
		
	}

}
